import {
    defineStore
} from 'pinia'
// import { useRouter } from 'vue-router'
// const router = useRouter()
export const useStore = defineStore('test', {
    state: () => {
        return {
            isCollse: false,
            current: null,
            tableList: [{

                path: 'home',
                name: 'home',
                label: '首页',
                icon: 'House',
                url: 'Home/index'

            }],
            loginRouters: [],
            token:''
        }
    },
    actions: {
        setRoutes(val:any) {
            this.loginRouters = val
            localStorage.setItem('menu', JSON.stringify(val))
           
        },
        addMenu(router:any) {
            if (!localStorage.getItem("menu")) {
            return
            }
            let stateRoutes = JSON.parse(localStorage.getItem('menu'))
            this.loginRouters = stateRoutes
            // let menuRouter = stateRoutes
            let menus:any = []
            stateRoutes.forEach((item:any) => {
                if (item.children) {
                    item.children.map((item:any) => {
                        let url = `../view/${item.url}.vue`
                        item.component = () => import(url)
                        return item
                    })
                    menus.push(...item.children)
                } else {
                    let url = `../view/${item.url}.vue`
                    item.component = () => import(url)
                    menus.push(item)
                }
            })
            // console.log(menus);
            
            menus.forEach((item:any) => {
                router.addRoute('home1', item)
            })
            // console.log(router.hasRoute('user'));
            
        },
        cleanRoutes(){
            this.loginRouters=[]
            localStorage.clear()
        },
        getToken(){
            this.token = localStorage.getItem('token') as string
        },
        setToken(val:any){
            this.token  = val
            localStorage.setItem('token',JSON.stringify(val))
        },
        clearToken(){
            this.token = ''
            localStorage.removeItem('token')
        }
    },
  
})